/* PixMap.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the PixMap class, which extends a surface allowing its
individual pixels to be edited.

Also defines ConstPixMap, a PixMap where individual pixel values can be
viewed but not modified.

PixMap and ConstPixMap are instances of PixMapT with a pixel type of Uint32.
*/

#ifndef __MZ_PIX_MAP_H__
#define __MZ_PIX_MAP_H__

#include "Surface.h"

#include <cassert>

template <typename Type>
class PixMapT;

template <typename Type>
class ConstPixMapT;

typedef PixMapT<Uint32> PixMap;
typedef ConstPixMapT<Uint32> ConstPixMap;



template <typename Type>
class ConstPixMapT {
public:
	ConstPixMapT(const Surface &surface)
		: surface(surface)
	{
		assert(sizeof(Type) == surface.data->format->BytesPerPixel);
		if(SDL_MUSTLOCK(surface.data))
			assert(SDL_LockSurface(surface.data) >= 0);

		pitch = surface.data->pitch / sizeof(Type);
		width = surface.Width();
		height = surface.Height();
		pixels = reinterpret_cast<const Type *>(surface.data->pixels) + 
			surface.bounds.y * pitch + surface.bounds.x;
	}
	~ConstPixMapT()
	{
		if(SDL_MUSTLOCK(surface.data))
			SDL_UnlockSurface(surface.data);
	}

	int Pitch() const { return pitch; }
	int Height() const { return height; }
	int Width() const { return width; }

	// Access the pixel data of this surface.
	// Note: PixMapT defines non-const versions of these three functions.
	const Type &operator()(int x, int y) const
	{
		return pixels[x + y * pitch];
	}
	const Type *Begin(int y) const
	{
		return pixels + y * pitch;
	}
	const Type *End(int y) const
	{
		return Begin(y) + width;
	}

	// Get the format of this surface.
	const SDL_PixelFormat &Format() const
	{
		return *surface.data->format;
	}


private:
	Surface surface;
	int pitch;
	int width;
	int height;
	const Type *pixels;
};



template <class Type>
class PixMapT : public ConstPixMapT<Type> {
public:
	PixMapT(Surface &surface) : ConstPixMapT<Type>(surface) {}

	using ConstPixMapT<Type>::operator();
	Type &operator()(int x, int y)
	{
		return const_cast<Type &>(
			ConstPixMapT<Type>::operator()(x, y));
	}

	using ConstPixMapT<Type>::Begin;
	Type *Begin(int y)
	{
		return const_cast<Type *>(
			ConstPixMapT<Type>::Begin(y));
	}

	using ConstPixMapT<Type>::End;
	Type *End(int y)
	{
		return const_cast<Type *>(
			ConstPixMapT<Type>::End(y));
	}
};



#endif



/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
